package com.byedu.jupiter.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;

/**
 * Created by jason on 2017/6/4.
 */
public class ExcelUtil {
    protected final static Logger logger = LoggerFactory.getLogger(ExcelUtil.class.getName());

    public static Workbook createWorkbook(File excelFile) {
        Workbook rwb = null;
        InputStream excelInput = null;
        try {
            excelInput = new FileInputStream(excelFile);
            rwb = new HSSFWorkbook(excelInput);
        } catch (Exception e) {
            try {
                excelInput = new FileInputStream(excelFile);
                rwb = new XSSFWorkbook(excelInput);
            } catch (Exception ex1) {
                logger.error("转换excel文件格式不正确", ex1);
            }
        } finally {
            try {
                if (excelInput != null) {
                    excelInput.close();
                }
            } catch (IOException e) {
                logger.error("流文件关闭异常 ，{}", e);
            }
        }
        return rwb;
    }

    public static Workbook create(InputStream in) {
        Workbook rwb = null;
        try {
            rwb = new HSSFWorkbook(in);
        } catch (Exception e) {
            try {
                rwb = new XSSFWorkbook(in);
            } catch (Exception ex1) {
                logger.error("转换excel文件格式不正确", ex1);
            }
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error("流文件关闭异常 ，{}", e);
            }
        }
        return rwb;
    }

    public static String getCellValue(Cell cell) {
        String cellValue = "";
        if (cell == null) {
            return cellValue;
        } else {
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    NumberFormat formater = NumberFormat.getInstance();
                    formater.setGroupingUsed(false);
                    cellValue = formater.format(cell.getNumericCellValue());
                    break;
                case Cell.CELL_TYPE_STRING:
                    cellValue = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    cellValue = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BLANK:
                    cellValue = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    cellValue = Boolean.valueOf(cell.getBooleanCellValue()).toString();
                    break;
                case Cell.CELL_TYPE_ERROR:
                    cellValue = String.valueOf(cell.getErrorCellValue());
                    break;
                default:
                    cellValue = "";
            }
        }
        return cellValue.trim();
    }
}
